package action.system;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import service.applycheck.ApplyUserService;
import service.credit.CreditApplyEnterpriseService;
import service.credit.CreditApplyPersonalService;
import service.creditmanage.CreditManageService;
import service.system.MenuService;
import util.Sessions;
import vo.InforHrefBean;
import core.action.BaseAction;
import core.global.Global;
import exception.Exceptions;

public class MenuAction extends BaseAction{
	private MenuService menuService;
	private String jsondata;
	private String page;
	
	private CreditApplyPersonalService creditApplyPersonalService;
	private CreditApplyEnterpriseService creditApplyEnterpriseService;
	private List<InforHrefBean> inforHrefBeanList;
	private ApplyUserService applyUserService;
	
	//for creditManageMap
	private CreditManageService creditManageService;
	private Map<String,Object> creditManageMap=new HashMap<String,Object>();
	
	public String execute(){
		return "success";
	}
	
	public String index() throws Exception{
		//初始化菜单
		this.initMenu();
		//待处理的放款条数 待处理回款条数 待处理的利息通知单条数		
		this.initCreditManageMap();
		//
		
		this.inforHrefBeanList = this.getCheckInforList();
		//System.out.println("inforHrefBeanList = " + inforHrefBeanList);
		return "success";
	}
	
	private void initMenu() throws Exception{
		String[] arrays={"home","custom","grant","credit","creditcheck","contract","creditout","creditmanage","report","system","manage"};
		for(String arr:arrays){
			Sessions.set(arr, menuService.getMenuJson(arr));
		}
	}
	
	public String sidebar() throws Exception{		
		jsondata=menuService.getMenuJson(page);
		HttpServletResponse response = ServletActionContext.getResponse();     
		response.setContentType("text/json;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		PrintWriter writer = response.getWriter();
		writer.write(jsondata);
		writer.flush();
		writer.close();
		return null;
	}
	
	public List<InforHrefBean> getCheckInforList() throws Exceptions{
		List<InforHrefBean> inforHrefBean = new ArrayList<InforHrefBean>();
		
		if(this.applyUserService.isAccept(Global.userid())){
			int n111 = this.creditApplyPersonalService.getNumberAllAcceptionList("wait");
			if(n111 != 0){
				inforHrefBean.add(new InforHrefBean("个人等待授理记录有：" + n111 + "条", "../creditcheck/creditAcceptAction", "cdw", 1));
			}
			
			int n121 = this.creditApplyEnterpriseService.getNumberAllAcceptionList("wait");
			if(n121 != 0){
				inforHrefBean.add(new InforHrefBean("企业等待授理记录有：" + n121 + "条", "../creditcheck/creditAcceptAction", "cdw", 2));
			}
		}
		
			int n211 = this.creditApplyPersonalService.getNumberAllSurveyList("wait");
			if(n211 != 0){
				inforHrefBean.add(new InforHrefBean("个人等待调查记录有：" + n211 + "条", "../creditcheck/creditSurveyAction", "cdw", 1));
			}
		
			int n221 = this.creditApplyEnterpriseService.getNumberAllSurveyList("wait");
			if(n221 != 0){
				inforHrefBean.add(new InforHrefBean("企业等待调查记录有：" + n221 + "条", "../creditcheck/creditSurveyAction", "cdw", 2));
			}
		
			int n212 = this.creditApplyPersonalService.getNumberAllSurveyList("returnBack");
			if(n212 != 0){
				inforHrefBean.add(new InforHrefBean("个人调查上级返回记录有：" + n212 + "条", "../creditcheck/creditSurveyAction", "cdr", 1));
			}
			
			int n222 = this.creditApplyEnterpriseService.getNumberAllSurveyList("returnBack");
			if(n222 != 0){
				inforHrefBean.add(new InforHrefBean("企业调查上级返回记录有：" + n222 + "条", "../creditcheck/creditSurveyAction", "cdr", 2));
			}
		
			int n311 = this.creditApplyPersonalService.getNumberAllExamineList("wait");
			if(n311 != 0){
				inforHrefBean.add(new InforHrefBean("个人等待审查记录有：" + n311 + "条", "../creditcheck/creditExamineAction", "cdw", 1));
			}
			
			int n321 = this.creditApplyEnterpriseService.getNumberAllExamineList("wait");
			if(n321 != 0){
				inforHrefBean.add(new InforHrefBean("企业等待审查记录有：" + n321 + "条", "../creditcheck/creditExamineAction", "cdw", 2));
			}
		
			int n312 = this.creditApplyPersonalService.getNumberAllExamineList("returnBack");
			if(n312 != 0){
				inforHrefBean.add(new InforHrefBean("个人审查上级返回记录有：" + n312 + "条", "../creditcheck/creditExamineAction", "cdr", 1));
			}
			
			int n322 = this.creditApplyEnterpriseService.getNumberAllExamineList("returnBack");
			if(n322 != 0){
				inforHrefBean.add(new InforHrefBean("企业审查上级返回记录有：" + n322 + "条", "../creditcheck/creditExamineAction", "cdr", 2));
			}
			
			int n411 = this.creditApplyPersonalService.getNumberAllExamineAdviceList("wait");
			if(n411 != 0){
				inforHrefBean.add(new InforHrefBean("个人等待复审记录有：" + n411 + "条", "../creditcheck/creditExamineAdviceAction", "cdw", 1));
			}
		
			int n421 = this.creditApplyEnterpriseService.getNumberAllExamineAdviceList("wait");
			if(n421 != 0){
				inforHrefBean.add(new InforHrefBean("企业等待复审记录有：" + n421 + "条", "../creditcheck/creditExamineAdviceAction", "cdw", 2));
			}
		
			int n511 = this.creditApplyPersonalService.getNumberAllApprovalList("wait");
			if(n511 != 0){
				inforHrefBean.add(new InforHrefBean("个人等待审批记录有：" + n511 + "条", "../creditcheck/creditApprovalAction", "cdw", 1));
			}
			
			int n521 = this.creditApplyEnterpriseService.getNumberAllApprovalList("wait");
			if(n521 != 0){
				inforHrefBean.add(new InforHrefBean("企业等待审批记录有：" + n521 + "条", "../creditcheck/creditApprovalAction", "cdw", 2));
			}
		
			int n512 = this.creditApplyPersonalService.getNumberAllApprovalList("returnBack");
			if(n512 != 0){
				inforHrefBean.add(new InforHrefBean("个人审批上级返回记录有：" + n512 + "条", "../creditcheck/creditApprovalAction", "cdr", 1));
			}
			
			int n522 = this.creditApplyEnterpriseService.getNumberAllApprovalList("returnBack");
			if(n522 != 0){
				inforHrefBean.add(new InforHrefBean("企业审批上级返回记录有：" + n522 + "条", "../creditcheck/creditApprovalAction", "cdr", 2));
			}
		
			int n611 = this.creditApplyPersonalService.getNumberAllFinalApprovalList("wait");
			if(n611 != 0){
				inforHrefBean.add(new InforHrefBean("个人等待终审记录有：" + n611 + "条", "../creditcheck/creditFinalApprovalAction", "cdw", 1));
			}
		
			int n621 = this.creditApplyEnterpriseService.getNumberAllFinalApprovalList("wait");
			if(n621 != 0){
				inforHrefBean.add(new InforHrefBean("企业等待终审记录有：" + n621 + "条", "../creditcheck/creditFinalApprovalAction", "cdw", 2));
			}
		return inforHrefBean;
	}
	
	public void initCreditManageMap (){
		creditManageMap.put("inundo", creditManageService.getInUndo());
		creditManageMap.put("outundo", creditManageService.getOutUndo());
		creditManageMap.put("interestundo", creditManageService.getInterestUndo());
		
		creditManageMap.put("inundone", creditManageService.getInUndone());
		creditManageMap.put("outundone", creditManageService.getOutUndone());
		creditManageMap.put("interestundone", creditManageService.getInterestUndone());
	}
	
	public MenuService getMenuService() {
		return menuService;
	}

	public void setMenuService(MenuService menuService) {
		this.menuService = menuService;
	}

	public String getJsondata() {
		return jsondata;
	}

	public void setJsondata(String jsondata) {
		this.jsondata = jsondata;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public CreditApplyPersonalService getCreditApplyPersonalService() {
		return creditApplyPersonalService;
	}

	public void setCreditApplyPersonalService(
			CreditApplyPersonalService creditApplyPersonalService) {
		this.creditApplyPersonalService = creditApplyPersonalService;
	}

	public CreditApplyEnterpriseService getCreditApplyEnterpriseService() {
		return creditApplyEnterpriseService;
	}

	public void setCreditApplyEnterpriseService(
			CreditApplyEnterpriseService creditApplyEnterpriseService) {
		this.creditApplyEnterpriseService = creditApplyEnterpriseService;
	}

	public List<InforHrefBean> getInforHrefBeanList() {
		return inforHrefBeanList;
	}

	public void setInforHrefBeanList(List<InforHrefBean> inforHrefBeanList) {
		this.inforHrefBeanList = inforHrefBeanList;
	}

	public Map<String,Object> getCreditManageMap() {
		return creditManageMap;
	}

	public void setCreditManageMap(Map<String,Object> creditManageMap) {
		this.creditManageMap = creditManageMap;
	}

	public CreditManageService getCreditManageService() {
		return creditManageService;
	}

	public void setCreditManageService(CreditManageService creditManageService) {
		this.creditManageService = creditManageService;
	}

	public ApplyUserService getApplyUserService() {
		return applyUserService;
	}

	public void setApplyUserService(ApplyUserService applyUserService) {
		this.applyUserService = applyUserService;
	}
}
